﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.fminutes.vinciNet {
    import flash.events.*;
    import flash.utils.*;
    import flash.net.*;

    public class FSocket extends Socket {

        private var _host:String;
        private var _port:int;
        private var _portList:Array;
        private var _portIndex:int = 0;
        private var _connected:Boolean = false;
        private var _receive:Boolean = false;
        private var timer:Timer;
        private var readHead:Boolean = false;
        private var cLength:int = 0;
        private var cCmd:int = 0;
        private var cRes:int = 0;
        private var cData:ByteArray;
        private var _connectStatus:int = 0;
        private var loginData:ByteArray;
        private var _pause:Boolean = false;
        private var dataList:Array;

        public function FSocket(){
            this.dataList = [];
            super();
            this.endian = Endian.LITTLE_ENDIAN;
            this.configureListeners();
        }
        private function configureListeners():void{
            addEventListener(Event.CONNECT, this.onConnect);
            addEventListener(ProgressEvent.SOCKET_DATA, this.onSocketData);
            addEventListener(Event.CLOSE, this.onClose);
            addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onSecurity);
            addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler);
        }
        public function get receive():Boolean{
            return (this._receive);
        }
        public function reConnect():void{
            if (this.pause){
                return;
            };
            if ((((this.connected == false)) && ((this._connectStatus == 0)))){
                this._connectStatus = 1;
                this.__connect(this._host, this._port);
            };
        }
        public function connectMulti(_arg1:String, _arg2:String):void{
            this._connectStatus = 1;
            this._host = _arg1;
            this._portList = _arg2.split("|");
            this._port = this._portList[this._portIndex];
            this.__connect(_arg1, this._port);
        }
        private function __connect(_arg1:String, _arg2:int):void{
            trace("socket 连接__host...................................", _arg1, _arg2);
            var _local3:SocketEvent = new SocketEvent(SocketEvent.TRACE_EVENT);
            _local3.traceValue = ((((("host:" + _arg1) + " port:") + _arg2) + " time:") + getTimer());
            dispatchEvent(_local3);
            super.connect(_arg1, _arg2);
            if (this.timer != null){
                this.timer.removeEventListener(TimerEvent.TIMER, this.onTimer);
            };
            this.timer = new Timer(6000, 1);
            this.timer.addEventListener(TimerEvent.TIMER, this.onTimer);
            this.timer.start();
        }
        private function onTimer(_arg1:TimerEvent):void{
            var _local2:SocketEvent = new SocketEvent(SocketEvent.TRACE_EVENT);
            _local2.traceValue = ("timeout:" + getTimer());
            dispatchEvent(_local2);
            if (this._receive == false){
                this.retrySocket();
            };
            (_arg1.currentTarget as Timer).removeEventListener(TimerEvent.TIMER, this.onTimer);
        }
        private function onConnect(_arg1:Event):void{
            var _local2:SocketEvent = new SocketEvent(SocketEvent.TRACE_EVENT);
            _local2.traceValue = ("onConnect:" + getTimer());
            dispatchEvent(_local2);
            this._connectStatus = 2;
            if (this.loginData != null){
                this.writeBytes(this.loginData);
                this.flush();
            };
            this.sendData();
            this._connected = true;
        }
        private function onClose(_arg1:Event):void{
            var _local2:SocketEvent = new SocketEvent(SocketEvent.TRACE_EVENT);
            _local2.traceValue = ("onClose:" + getTimer());
            dispatchEvent(_local2);
            if (this.retrySocket() == false){
                dispatchEvent(new SocketEvent(SocketEvent.SOCKET_CLOSE));
                this._connectStatus = 0;
            };
        }
        private function onSecurity(_arg1:SecurityErrorEvent):void{
            var _local2:SocketEvent = new SocketEvent(SocketEvent.TRACE_EVENT);
            _local2.traceValue = ("onSecurity:" + getTimer());
            dispatchEvent(_local2);
            if (this.retrySocket() == false){
                this._connectStatus = 0;
            };
        }
        private function ioErrorHandler(_arg1:IOErrorEvent):void{
            var _local2:SocketEvent = new SocketEvent(SocketEvent.TRACE_EVENT);
            _local2.traceValue = ("ioError:" + getTimer());
            dispatchEvent(_local2);
            this.retrySocket();
        }
        private function retrySocket():Boolean{
            if ((((this._portIndex >= (this._portList.length - 1))) || ((this._receive == true)))){
                return (false);
            };
            this._portIndex = (this._portIndex + 1);
            this._port = this._portList[this._portIndex];
            this.__connect(this._host, this._port);
            return (true);
        }
        private function onSocketData(_arg1:ProgressEvent):void{
            var _local3:SocketEvent;
            var _local2:SocketEvent = new SocketEvent(SocketEvent.TRACE_EVENT);
            _local2.traceValue = "onData";
            dispatchEvent(_local2);
            this._receive = true;
            while (this.bytesAvailable > 0) {
                if (!this.readHead){
                    if (this.bytesAvailable >= 8){
                        this.cLength = this.readUnsignedInt();
                        this.cCmd = this.readUnsignedShort();
                        this.cRes = this.readUnsignedShort();
                        this.readHead = true;
                    } else {
                        break;
                    };
                };
                if (this.readHead){
                    if (this.bytesAvailable >= this.cLength){
                        if (this.cLength > 0){
                            this.cData = new ByteArray();
                            this.readBytes(this.cData, 0, this.cLength);
                        } else {
                            this.cData = null;
                        };
                        _local3 = new SocketEvent(SocketEvent.DATA_COMP);
                        _local3.cmd = this.cCmd;
                        _local3.data = this.cData;
                        _local3.res = this.cRes;
                        dispatchEvent(_local3);
                        this.readHead = false;
                    } else {
                        break;
                    };
                };
            };
        }
        public function send(_arg1:int, _arg2:ByteArray, _arg3:int=0, _arg4:String=""):void{
            var _local5:SocketEvent = new SocketEvent(SocketEvent.TRACE_EVENT);
            _local5.traceValue = "send";
            dispatchEvent(_local5);
            var _local6:uint = _arg2.length;
            var _local7:ByteArray = new ByteArray();
            _local7.endian = Endian.LITTLE_ENDIAN;
            _local7.writeUnsignedInt(_local6);
            _local7.writeShort(_arg1);
            _local7.writeShort(_arg3);
            _local7.writeBytes(_arg2);
            if (this.loginData == null){
                this.loginData = _local7;
                if (this._connected){
                    this.writeBytes(this.loginData);
                    this.flush();
                };
            } else {
                this.dataList.push(_local7);
                this.sendData();
            };
        }
        public function get pause():Boolean{
            return (this._pause);
        }
        public function set pause(_arg1:Boolean):void{
            if (((_arg1) && (this.connected))){
                this.close();
            };
            this._pause = _arg1;
        }
        private function sendData():void{
            var _local1:ByteArray;
            if (this.pause){
                return;
            };
            if (this.connected){
                while (this.dataList.length > 0) {
                    _local1 = (this.dataList.shift() as ByteArray);
                    this.writeBytes(_local1);
                    this.flush();
                };
            } else {
                if (this._connectStatus == 0){
                    this.__connect(this._host, this._port);
                };
            };
        }

    }
}//package com.fminutes.vinciNet 
